<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .login-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 400px;
            padding: 40px;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #777;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: all 0.3s;
        }

        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
            outline: none;
        }

        .btn {
            display: block;
            width: 100%;
            padding: 12px;
            background: linear-gradient(to right, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .btn:active {
            transform: translateY(0);
        }

        .alert {
            padding: 12px;
            border-radius: 5px;
            margin-top: 20px;
            display: none;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .loading {
            display: none;
            text-align: center;
            margin-top: 10px;
        }

        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin-right: 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .demo-info {
            margin-top: 20px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 5px;
            font-size: 14px;
            color: #6c757d;
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <h1>系统登录</h1>
        <p>请输入您的用户名和密码</p>
    </div>

    <form id="loginForm">
        <div class="form-group">
            <label for="uname">用户名</label>
            <input type="text" id="uname" name="uname" class="form-control" placeholder="请输入用户名" required>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" class="form-control" placeholder="请输入密码" required>
        </div>

        <button type="submit" class="btn">登录</button>
    </form>

    <div class="loading" id="loading">
        <div class="spinner"></div>
        正在登录，请稍候...
    </div>

    <div class="alert alert-success" id="successAlert">
        登录成功！正在跳转...
    </div>

    <div class="alert alert-danger" id="errorAlert">
        登录失败，请检查用户名和密码！
    </div>

    <div class="demo-info">
        <p><strong>测试账号：</strong></p>
        <p>用户名：admin</p>
        <p>密码：1234</p>
    </div>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();

        // 获取表单数据
        const uname = document.getElementById('uname').value;
        const password = document.getElementById('password').value;

        // 显示加载中
        document.getElementById('loading').style.display = 'block';
        document.getElementById('successAlert').style.display = 'none';
        document.getElementById('errorAlert').style.display = 'none';

        // 发送登录请求
        fetch('login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: `uname=${encodeURIComponent(uname)}&password=${encodeURIComponent(password)}`
        })
            .then(response => {
                // 获取响应头中的token
                const token = response.headers.get('token');
                if (token) {
                    console.log('收到Token:', token);
                    // 可以将token保存到localStorage或sessionStorage中
                    localStorage.setItem('authToken', token);
                }

                return response.json();
            })
            .then(data => {
                // 隐藏加载中
                document.getElementById('loading').style.display = 'none';

                if (data.success) {
                    // 登录成功
                    document.getElementById('successAlert').style.display = 'block';
                    document.getElementById('successAlert').textContent = data.msg || '登录成功！';

                    // 模拟跳转
                    setTimeout(() => {
                        alert('登录成功！Token已保存。在实际应用中，这里会跳转到系统首页。');
                    }, 1500);
                } else {
                    // 登录失败
                    document.getElementById('errorAlert').style.display = 'block';
                    document.getElementById('errorAlert').textContent = data.msg || '登录失败！';
                }
            })
            .catch(error => {
                // 隐藏加载中
                document.getElementById('loading').style.display = 'none';

                // 显示错误信息
                document.getElementById('errorAlert').style.display = 'block';
                document.getElementById('errorAlert').textContent = '网络错误，请稍后重试！';
                console.error('登录请求失败:', error);
            });
    });
</script>
</body>
</html>